﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Event Source Tests</title>
    <style>
        body {
            font-family: Arial;
        }

        p {
            margin: 0;
        }
        
        .open {
            color: #08a41c;
        }
        
        .err {
            color: #f00;
        }
    </style>
</head>
<body>
    <div id="log"></div>

    <script src="../Scripts/jquery-1.8.2.js"></script>
    <script src="../Scripts/jquery.signalR.js"></script>
    <script>
        $(function () {
            var connection = $.connection("Raw/Raw", null, true),
                $log = $("#log");

            function log(cat, msg) {
                if (!msg) {
                    msg = cat;
                    cat = "";
                }
                $log.append("<p class='" + cat + "'>" + msg + "</p>");
            }

            function getReadyState(id) {
                if (id === window.EventSource.CONNECTING) {
                    return "CONNECTING (" + id + ")";
                } else if (id === window.EventSource.OPEN) {
                    return "OPEN (" + id + ")";
                } else if (id === window.EventSource.CLOSED) {
                    return "CLOSED (" + id + ")";
                }
            }

            function onStart() {
                // http://localhost:40476/Raw/Raw/Raw/connect?transport=serverSentEvents&connectionId=8bdaa3a5-975a-4826-bd06-26ea7d51c026
                var url = connection.url + "?transport=serverSentEvents&connectionId=" + connection.id + "&bogus=true",
                    es;

                //window.setTimeout(function() { connection.stop(); }, 100);

                log("EventSource url: " + url);

                es = new window.EventSource(url);
                es.addEventListener("open", function (e) {
                    log("open", "EventSource: opened, readyState=" + getReadyState(es.readyState));
                });
                es.addEventListener("message", function (e) {
                    log("messge", "EventSource: message, " +
                        (e.data === "initialized" ? e.data : "timedOut=" + window.JSON.parse(e.data).TimedOut));
                });
                es.addEventListener("error", function (e) {
                    log("err", "EventSource: error, eventPhase=" + getReadyState(e.eventPhase) + ", readyState=" + getReadyState(es.readyState));
                });
            }

            connection.start().done(onStart);
        });
    </script>
</body>
</html>
